package cn.com.djin.ssm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.djin.ssm.service.AddressService;
import cn.com.djin.ssm.service.BaseService;
import cn.com.djin.ssm.service.CarService;
import cn.com.djin.ssm.service.GoodColorService;
import cn.com.djin.ssm.service.GoodEvaluateService;
import cn.com.djin.ssm.service.GoodNumService;
import cn.com.djin.ssm.service.GoodService;
import cn.com.djin.ssm.service.GoodSizeService;
import cn.com.djin.ssm.service.GoodTypeService;
import cn.com.djin.ssm.service.GpictureService;
import cn.com.djin.ssm.service.UserService;

/**
 * 
 * @author djin 基础控制器层
 * @param <T>
 *            实体类泛型
 */
public class BaseController<T> {

	// 定义操作结果常量
	// 操作成功
	protected static final String SUCCESS = "success";
	// 操作失败
	protected static final String FAIL = "fail";
	// 操作异常
	protected static final String ERROR = "error";
	// 后台登录异常
	protected static final String HTJURISDICTION = "htJurisdiction";

	// 定义list集合
	protected List<T> list = new ArrayList<T>();
	// 做Layui分页的map集合
	protected Map<String, Object> jsonMap = new HashMap<String, Object>();
	// 普通的map集合
	protected Map<String, Object> map = new HashMap<String, Object>();

	// 基础业务层对象
	@Autowired
	protected BaseService<T> baseService;
	// 用户业务层对象
	@Autowired
	protected UserService userService;
	// 用户业务层对象
	@Autowired
	protected GoodService goodService;

	// 商品业务层对象
	@Autowired
	protected GoodTypeService goodTypeService;
	// 商品业务层对象
	@Autowired
	protected GoodSizeService goodSizeService;
	// 商品业务层对象
	@Autowired
	protected GoodColorService goodColorService;

	// 商品业务层对象
	@Autowired
	protected GoodNumService goodNumService;

	// 商品业务层对象
	@Autowired
	protected GpictureService gpictureService;
	
	@Autowired
	protected GoodEvaluateService goodEvaluateService;
	
	@Autowired
	protected AddressService addressService;
	@Autowired
	protected CarService carService;

	/**
	 * 查询所有
	 * 
	 * @param t
	 *            参数对象
	 * @return 操作结果
	 */
	@RequestMapping("/queryTotalByPramas")
	@ResponseBody
	public String queryTotalByPramas(T t) {
		try {
			baseService.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return null;
	}

	/**
	 * 添加
	 * 
	 * @param t
	 *            参数对象
	 * @return 操作结果
	 */
	@RequestMapping("/saveT")
	@ResponseBody
	public String saveT(T t) {
		try {
			System.out.println("数据"+t);
			return baseService.saveT(t);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 修改任意字段
	 * 
	 * @param t
	 *            参数对象
	 * @return 操作结果
	 */
	@RequestMapping("/updT")
	@ResponseBody
	public String updT(T t) {
		try {
			return baseService.updByPramas(t);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 根据id删除单个结果
	 * 
	 * @param t
	 *            参数对象
	 * @return 操作结果
	 */
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(String id) {
		try {
			return baseService.removeById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 根据id批量删除
	 * 
	 * @param ids
	 *            数据id数组
	 * @return 删除结果
	 */
	@RequestMapping("/deletes")
	@ResponseBody
	public String deletes(String[] params) {
		try {
			return baseService.removeBatch(params);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 根据有（无）条件分页查询
	 * 
	 * @param t
	 *            参数对象
	 * @param page
	 *            当前页
	 * @param limit
	 *            每一页显示的数据条数
	 * @return Map数据集合
	 */
	@RequestMapping("/listByPramas")
	@ResponseBody
	public Map<String, Object> listByPramas(T t, Integer page, Integer limit) {
		try {
			jsonMap = baseService.findLsitByPramas(t, page, limit);
			jsonMap.put("code", 0);
			jsonMap.put("msg", "数据访问成功");
			return jsonMap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据多个条件查询单个数据
	 * 
	 * @param t
	 *            条件对象参数
	 * @return 数据对象
	 * @throws Exception
	 */
	@RequestMapping("/loadTByPramas")
	@ResponseBody
	public T loadTByPramas(T t) {
		try {
			return baseService.findTByPramas(t);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/findManyByPramas")
	@ResponseBody
	public List<T> findManyByPramas(T t){	
		try {
			return baseService.findManyByPramas(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 根据条件查询单个结果
	 * 
	 */
	@RequestMapping("/loadObjectByPramas")
	@ResponseBody
	public T loadObjectByPramas(T t){
		try {
			return baseService.findObjectByPramas(t);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	} 
}
